-------------------------------------------------------------------------------------------------------
---- 辅助RPC系统，用来快速交互服务器和客户端数据
---- 警告：尽量不要在同一次FPS里执行多个RPC，会导致信道阻塞
---- 部分交互API在 07_npc_lib_rpc.lua 里注册

-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------------------
------  PushEvent 的客户端 /服务端 添加，方便数据/事件交互
    AddClientModRPCHandler("NPC_RPC_SYSTEM","PushEvent",function(inst,event_name,data)  ----- Register on the client
        -- use in server ：inst.npc_base_lib:RPC_PushEvent("event_name",data)
        -- 广播下发给所有客户端 PushEvent
        if inst then
            inst:PushEvent(event_name,json.decode(data))
        end
    end)


    AddModRPCHandler("NPC_RPC_SYSTEM", "PushEvent.from_client", function(player_inst,inst,event_name,data_json) ----- Register on the server
        -- user in client : inst.replica.npc_base_lib:PushEvent("event_name",data)
        -- 客户端回传 event 给 服务端
        if inst and inst.PushEvent and event_name then
            local data = nil
            if data_json then
                data = json.decode(data_json)
            end
            inst:PushEvent(event_name,data)
        else
            print("RPC PushEvent.from_client ERROR")
        end
    end)

-------------------------------------------------------------------------------------------------------
------  tags add / remove
    AddClientModRPCHandler("NPC_RPC_SYSTEM","AddTags",function(inst,data)
        local tags = json.decode(data)
        if inst and inst.AddTag then
            for k, v in pairs(tags) do
                inst:AddTag(v)
            end
        end
    end)
    AddClientModRPCHandler("NPC_RPC_SYSTEM","RemoveTags",function(inst,data)
        local tags = json.decode(data)
        if inst and inst.RemoveTag then
            for k, v in pairs(tags) do
                inst:RemoveTag(v)
            end
        end
    end)

    AddClientModRPCHandler("NPC_RPC_SYSTEM","ChangeTags",function(inst,data)
        local tags_cmd = json.decode(data)
        if inst and inst.RemoveTag and inst.AddTag then

            if tags_cmd.Add and type(tags_cmd.Add) == "table" then
                for k, v in pairs(tags_cmd.Add) do
                    inst:AddTag(v)
                end
            end
            
            if tags_cmd.Remove and type(tags_cmd.Remove) == "table" then
                for k, v in pairs(tags_cmd.Remove) do
                    inst:RemoveTag(v)
                end
            end

        end
    end)
-------------------------------------------------------------------------------------------------------
AddShardModRPCHandler("NPC_RPC_SYSTEM", "World_Event_Through_The_Cave", function(shardId,event_name, data_json)
    TheWorld:PushEvent(event_name,data_json and json.decode(data_json))
end)
-- SendModRPCToShard(SHARD_MOD_RPC["NPC_RPC_SYSTEM"]["World_Event_Through_The_Cave"], shardId ,...)	--- shardId 直接为nil就可以跨洞穴执行

-------------------------------------------------------------------------------------------------------

AddReplicableComponent("npc_base_lib")------ 注册lib 的 replica